<template>
    <view class="container">
        <view v-if="recommendations" class="recommendation-container">
            <view class="section">
                <h2 class="section-title">城市介绍</h2>
                <p class="section-content">{{ recommendations.data.TravelGuide.CityIntroduction }}</p>
            </view>
            <view class="section">
                <h2 class="section-title">预算</h2>
                <p class="section-content">{{ recommendations.data.TravelGuide.Budget }}</p>
            </view>
            <view class="section">
                <h2 class="section-title">美食推荐</h2>
                <view v-for="(food, index) in recommendations.data.TravelGuide.FoodRecommendations" :key="index" class="item">
                    <h3 class="item-title">{{ food.Name }}</h3>
                    <p class="item-description">{{ food.Description }}</p>
                    <p class="item-location">{{ food.Location }}</p>
                </view>
            </view>
            <view class="section">
                <h2 class="section-title">历史景点</h2>
                <view v-for="(site, index) in recommendations.data.TravelGuide.HistoricalSites" :key="index" class="item">
                    <h3 class="item-title">{{ site.Name }}</h3>
                    <p class="item-description">{{ site.Description }}</p>
                    <p class="item-location">{{ site.Location }}</p>
                </view>
            </view>
            <view class="section">
                <h2 class="section-title">娱乐活动</h2>
                <view v-for="(event, index) in recommendations.data.TravelGuide.Entertainment" :key="index" class="item">
                    <h3 class="item-title">{{ event.Name }}</h3>
                    <p class="item-description">{{ event.Description }}</p>
                    <p class="item-location">{{ event.Location }}</p>
                </view>
            </view>
        </view>
        <view v-if="errorMessage" class="error-message">{{ errorMessage }}</view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            recommendations: null,
            errorMessage: '',
            CityIntroduction: '',
            Budget: '',
            Name: '',
            Description: '',
            Location: ''
        };
    },
    methods: {
        goHome() {
            uni.reLaunch({
                url: '/pages/index/index'
            });
        },
        fetchRecommendations(city) {
            let lock = false;
            if (lock === false) {
                uni.showLoading({
                    title: 'AI规划中'
                });
            }
            uni.request({
                url: "https://api.xiaoyaoyou.hanfenga7.cn/api/v1/recommend/ai",
                dataType: "json",
                responseType: "json",
                method: 'POST',
                header: {
                    'Authorization': `Bearer ${uni.getStorageSync('token')}`,
                    'Content-Type': 'application/json'
                },
                data: {
                    city: city,
                    CityIntroduction: this.CityIntroduction,
                    Budget: this.Budget,
                    Name: this.Name,
                    Description: this.Description,
                    Location: this.Location
                },
                success: (res) => {
                    if (res.data.code === 200) {
                        console.log(res.data.code);
                        lock = true;
                        uni.showToast({
                            title: '规划完成',
                            icon:'success'
                        });
                        setTimeout(() => {
                            uni.redirectTo({
                                url: '/pages/itinerary/Itinerary'
                            });
                        }, 1000);
                        this.recommendations = res.data;
                    }
                },
                fail: (err) => {
                    this.errorMessage = '请求失败，请检查网络连接';
                }
            });
        }
    },
    onLoad(option) {
        console.log(option);
        this.fetchRecommendations(option.city);
    }
};
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

.container {
    padding: 2rem;
    font-family: 'Inter', sans-serif;
    background-color: #f9fafb;
    min-height: 100vh;
}

.back-home-btn {
    padding: 0.75rem 1.5rem;
    background-color: #3b82f6;
    color: white;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s ease;
    margin-bottom: 1.5rem;
}

.back-home-btn:hover {
    background-color: #2563eb;
}

.recommendation-container {
    background-color: #ffffff;
    border: none;
    border-radius: 0.75rem;
    padding: 1.5rem;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.section {
    margin-bottom: 1.5rem;
}

.section-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.75rem;
}

.section-content {
    color: #6b7280;
    line-height: 1.5;
}

.item {
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    padding: 1rem;
    margin-bottom: 1rem;
    transition: transform 0.2s ease;
}

.item:hover {
    transform: translateY(-0.25rem);
}

.item-title {
    font-size: 1rem;
    font-weight: 500;
    color: #1f2937;
    margin-bottom: 0.25rem;
}

.item-description {
    color: #6b7280;
    margin-bottom: 0.25rem;
}

.item-location {
    color: #9ca3af;
}

.error-message {
    color: #ef4444;
    margin-top: 1rem;
    font-size: 0.875rem;
}
</style>    